package com.ruoyi.project.cms.type.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.cms.commons.CMSConst;
import com.ruoyi.project.cms.type.domain.Type;
import com.ruoyi.project.cms.type.service.IAddTypeService;
import com.ruoyi.project.cms.type.service.IEditTypeService;

import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



@Controller
@RequestMapping("/cms/type")
public class EditTypeController extends BaseController {

    private String prifex="cms/type";
    @Autowired
    private IAddTypeService addTypeService;


    @Autowired
    private IEditTypeService editTypeService;
    /**
     * 修改类型
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        mmap.put("type", editTypeService.selectTypeById(typeId));
        return prifex + "/edit";
    }

    /**
     * 修改保存类型
     */
    @RequiresPermissions("cms:type:edit")
    @Log(title = "类型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Type type){
        if (CMSConst.TYPE_NAME_NOT_UNIQUE.equals(addTypeService.checkTypeNameUnique(type))){

            return error("修改类型'" + type.getTypeName() + "'失败，类型名称已存在");
        }

        return toAjax(editTypeService.updateType(type));
    }

}
